package com.domain.controller;

import com.domain.common.AppLogger;
import com.domain.common.Comments;
import com.domain.common.Result;
import com.domain.service.PublishService;
import com.domain.service.PublishTypeService;
import com.domain.utils.JwtUtils;
import org.apache.log4j.Level;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 发布问题 控制器
 */
@Controller
@RequestMapping("/publish")
public class PublishController {

    /**
     * 日志
     */
    private static org.apache.log4j.Logger logger = AppLogger.getLogger(IndexControler.class.getSimpleName(), Level.INFO.toString(), true);

    @Autowired
    private PublishTypeService publishTypeService;
    @Autowired
    private PublishService publishService;

    /**
     * 获取发布类型
     *
     * @param userToken
     * @return
     */
    @RequestMapping(value = "/getPublishType", method = RequestMethod.POST)
    @ResponseBody
    public Result getPublishType(String userToken) {
        try {
            //判断用户是否登录
            if (!JwtUtils.veryfiy(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return publishTypeService.getPublishType();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误！");
        }
    }


    /**
     * 发布帮忙
     *
     * @param userToken      用户token
     * @param userId         发布人id
     * @param typeId         发布类型id
     * @param publishTitle   发布标题
     * @param publishContent 发布内容
     * @param filterUserId   过滤好友id(多个逗号隔开)
     * @param longitude      发布位置经度
     * @param latitude       发布位置纬度
     * @param cityName       发布位置城市名称
     * @param itIsPaid       发布是否有偿
     * @param images         发布照片（多张照片逗号隔开）
     * @param money          帮转红包金额（没有传0.00）
     * @param
     * @return
     */
    @RequestMapping(value = "/savePublish", method = RequestMethod.POST)
    @ResponseBody
    public Result savePublish(String userToken,
                              String userId,
                              String typeId,
                              String publishTitle,
                              String publishContent,
                              String filterUserId,
                              String longitude,
                              String latitude,
                              String itIsPaid,
                              String cityName,
                              String images,
                              String money) {

        try {
            //判断用户是否登录
            if (!JwtUtils.veryfiy(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return publishService.savePublish(userId, typeId, publishTitle, publishContent, filterUserId, longitude, latitude, itIsPaid, cityName, images, money);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }

    /**
     * 公益发布
     *
     * @return
     */
    @RequestMapping(value = "/addPublishWelfare", method = RequestMethod.POST)
    @ResponseBody
    public Result addPublishWelfare(String strJson) {
        try {
            return publishService.addPublishWelfare(strJson);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }


    /**
     * 过滤得好友列表
     *
     * @param userToken 用户token
     * @param usersId   用户id
     * @param phoneNum  电话号码（搜索时传入）
     * @param name      名称（搜索时传入）
     * @param pageNum   页码
     * @param pageSize  页长度
     * @param request
     * @return
     */
    @RequestMapping(value = "/getFriendsLists", method = RequestMethod.POST)
    @ResponseBody
    public Result getFriendsLists(String userToken, String usersId, String phoneNum, String name, String pageNum, String pageSize, HttpServletRequest request) {
        try {
            //判断用户是否登录
            if (!JwtUtils.veryfiy(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return publishService.getFriendsLists(usersId, phoneNum, name, pageNum, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }

    /**
     * 完成帮忙判断
     *
     * @param userToken
     * @param userId
     * @param publishId
     * @return
     */
    @RequestMapping(value = "/judgeCompletePublish", method = RequestMethod.POST)
    @ResponseBody
    public Result judgeCompletePublish(String userToken, String userId, String publishId) {
        try {
            //判断用户是否登录
            if (!JwtUtils.veryfiy(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return publishService.judgeCompletePublish(userId, publishId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }


    /**
     * 完成帮忙
     *
     * @param userToken 用户token
     * @param userId    用户id
     * @param publishId 发布id
     * @param groupId   选择得创建群组得id
     * @return
     */
    @RequestMapping(value = "/completePublish", method = RequestMethod.POST)
    @ResponseBody
    public Result completePublish(String userToken, String userId, String publishId, String groupId) {
        try {
            //判断用户是否登录
            if (!JwtUtils.veryfiy(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return publishService.completePublish(userId, publishId, groupId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统失败");
        }
    }

    /**
     * 帮忙完成获取群id
     *
     * @param userToken
     * @param publishId
     * @param usersId
     * @return
     */
    @RequestMapping(value = "/getGroupIid", method = RequestMethod.POST)
    @ResponseBody
    public Result getGroupIid(String userToken, String publishId, String usersId) {
        try {
            //判断用户是否登录
            if (!JwtUtils.veryfiy(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return publishService.getGroupIid(publishId, usersId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统失败");
        }
    }

    /**
     * 帮转红包支付
     *
     * @param userToken    用户token
     * @param userId       用户id
     * @param total_amount 总金额
     * @return
     */
    @RequestMapping(value = "/helpRedPacakgePay", method = RequestMethod.POST)
    @ResponseBody
    public Result helpRedPacakgePay(String userToken, String userId, String total_amount) {
        try {
            //判断用户是否登录
            if (!JwtUtils.veryfiy(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return publishService.helpRedPacakgePay(userId, total_amount);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }

    /**
     * 帮转红包支付退款
     *
     * @param userToken   用户token
     * @param userId      用户id
     * @param returnMoney 退款钱数
     * @return
     */
    @RequestMapping(value = "/payReturn", method = RequestMethod.POST)
    @ResponseBody
    public Result payReturn(String userToken, String userId, String returnMoney) {
        try {
            //判断用户是否登录
            if (!JwtUtils.veryfiy(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return publishService.payReturn(userId, returnMoney);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }


    /**
     * 发布补加红包支付
     *
     * @param userToken    用户token
     * @param userId       用户id
     * @param total_amount 补加的钱数
     * @param publishId    发布id
     * @return
     */
    @RequestMapping(value = "/helpRedPacakgeSupplement", method = RequestMethod.POST)
    @ResponseBody
    public Result helpRedPacakgeSupplement(String userToken, String userId, String total_amount, String publishId) {
        try {
            //判断用户是否登录
            if (!JwtUtils.veryfiy(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return publishService.helpRedPacakgeSupplement(userId, total_amount, publishId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }

    /**
     * 完成帮忙答谢支付
     *
     * @param userToken
     * @param userId
     * @param total_amount
     * @param acUsersId
     * @return
     */
    @RequestMapping(value = "/acknowledge", method = RequestMethod.POST)
    @ResponseBody
    public Result acknowledge(String userToken, String userId, String total_amount, String acUsersId) {
        try {
            //判断用户是否登录
            if (!JwtUtils.veryfiy(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return publishService.acknowledge(userId, total_amount, acUsersId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }

}
